package logic

import (
	"context"
	"fmt"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreByIdLogic {
	return &StoreByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StoreByIdLogic) StoreById(in *agentClient.IdBody) (*agentClient.Store, error) {
	store, err := l.svcCtx.StoreModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			logx.Infof("StoreById 找不到该商户 (%d)", in.Id)
			return nil, errorx.NewDefaultError(fmt.Sprintf("找不到该商户。%d", in.Id))
		}
		return nil, err
	}

	return &agentClient.Store{
		Id:              store.Id,
		Platform:        store.Platform,
		Name:            store.Name,
		StoreId:         store.StoreId,
		StoreTypeId:     store.StoreTypeId,
		AgentId:         store.AgentId,
		Status:          store.Status,
		BusinessLicense: store.BusinessLicense,
		IndustryLicense: store.IndustryLicense,
		EquityPrice:     store.EquityPrice,
		StorePhone:      store.StorePhone,
		SupportTakeaway: store.SupportTakeaway,
		PackingFee:      store.PackingFee,
		CreateTime:      store.CreateTime.Unix(),
		UpdateTime:      store.UpdateTime.Unix(),
		DelFlag:         store.DelFlag,
		FirstPass:       store.FirstPass,
	}, nil
}
